package com.datasure.domain.service.impl;

import com.datasure.domain.entity.Device;
import com.datasure.domain.entity.DeviceEx;
import com.datasure.domain.entity.DiskClone;
import com.datasure.domain.entity.DiskCloneEx;
import com.datasure.domain.service.SourceDeviceDomainService;
import com.datasure.repository.DeviceExRepository;
import com.datasure.repository.DeviceRepository;
import com.datasure.repository.DiskCloneExRepository;
import com.datasure.repository.DiskCloneRepository;

import javax.xml.bind.ValidationException;
import java.util.Objects;

/**
 * @author zhangxiaolong
 * @ClassName DeviceDomainServiceImpl.java
 * @Description TODO
 * @createTime 2022年02月10日 14:40:00
 */
public class SourceDeviceDomainServiceImpl implements SourceDeviceDomainService {
    private DeviceRepository deviceRepository;
    private DeviceExRepository deviceExRepository;
    private DiskCloneRepository diskCloneRepository;
    private DiskCloneExRepository diskCloneExRepository;

    @Override
    public boolean sourceHasTask(Device device) {
        DeviceEx deviceEx = deviceExRepository.find(device.getDeviceUniqueId());
        /*return  sourceDevice != null && ((sourceDevice.getDeviceEx() != null
                && sourceDevice.getDeviceEx().getDeviceBindStatus() != null
                && sourceDevice.getDeviceEx().getDeviceBindStatus() == 1
                && sourceDevice.getDeviceEx().getDeviceBindUniqueId() != null) ||
                !StringUtils.isEmpty(sourceDevice.getOneKeyTaskName()));*/
        return false;
    }

    @Override
    public String getSourceTaskName(Device device) {
        DiskClone diskClone = diskCloneRepository.findByDeviceId(device.getDeviceId());
        DiskCloneEx diskCloneEx = diskCloneExRepository.find(diskClone.getDiskcloneId());

        /*sourceDeviceView.setIcon("css");
        if (device.getDiskClone().getDiskCloneEx() != null) {
            sourceDeviceView.setAssociatedTask(device.getDiskClone().getDiskCloneEx().getTaskName());

            sourceDeviceView.setDiskCloneId(device.getDiskClone().getDiskcloneId());
        }
        //有一键迁移任务 add by chuangchao 2021/6/4
        if (!StringUtils.isEmpty(device.getOneKeyTaskName())) {
            sourceDeviceView.setAssociatedTask(device.getOneKeyTaskName());
            sourceDeviceView.setDiskCloneId(device.getDiskClone().getDiskcloneId());
        }*/

        return "";
    }

    @Override
    public int getSourceState(Device device) {
        DiskClone diskClone = diskCloneRepository.findByDeviceId(device.getDeviceId());

        //未授权状态判断

        //迁移中

        //代交割

        //已交割

        //。。。。

        return 0;
    }

    public boolean hasSourceDevice(Device device){
        Device deviceDb = deviceRepository.findByDeviceUniqueId(device.getDeviceUniqueId());
        return Objects.nonNull(deviceDb) && Objects.nonNull(deviceDb.getDeviceId());
    }
}
